package com.free.explayer.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;


public class PermissionManager {

    private static final String TAG = "PermissionManager";
    public static final int REQUEST_READ_EXTERNAL_STORAGE_CODE = 101;
    public static final String READ_EXTERNAL_STORAGE_PERMISSION = Manifest.permission.READ_EXTERNAL_STORAGE;

    public static void requestReadExternalStoragePermission(Activity activity) {
        ActivityCompat.requestPermissions(activity, new String[]{READ_EXTERNAL_STORAGE_PERMISSION}, REQUEST_READ_EXTERNAL_STORAGE_CODE);
    }

    public static boolean onRequestReadExternalStoragePermission(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        for (int i = 0; i < permissions.length; i++) {
            String p = permissions[i];
            if (TextUtils.equals(p, READ_EXTERNAL_STORAGE_PERMISSION) && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }

    public static boolean isGrantedReadExternalStoragePermission(Context context) {
        return ActivityCompat.checkSelfPermission(context, READ_EXTERNAL_STORAGE_PERMISSION) == PackageManager.PERMISSION_GRANTED;
    }
}
